//
//  AuthViewController.m
//  CC
//
//  Created by Calvin on 4/27/15.
//  Copyright (c) 2015 Calvin. All rights reserved.
//

#import "AuthViewController.h"
#import <UIKit/UIKit.h>
#import "AFNetworking.h"
#import "TabBarViewController.h"
#import "ControllerTool.h"
#import "AccountModel.h"
#import "AccountTool.h"
@interface AuthViewController() <UIWebViewDelegate>

@end

@implementation AuthViewController
/**
 *  @author Calvin.wang, 15-05-17 20:05:23
 *
 *  <#Description#>
 */
-(void)viewDidLoad
{
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame=self.view.bounds;
    [self.view addSubview:webView];
    NSString *key =AppKey;
    NSString *redirectURL=RedirectURI;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",key,redirectURL]];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [webView loadRequest:request];
    webView.delegate=self;
}

#pragma mark-UIWebViewDelegate
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *urlStr = request.URL.absoluteString;
    NSLog(@"%@",urlStr);
    NSRange range = [urlStr rangeOfString:@"http://www.baidu.com/?code="];
    if (range.location != NSNotFound) {
        NSInteger from = range.location + range.length;
        NSString *code = [urlStr substringFromIndex:from];
        [self accessTokenWithCode:code];
        NSLog(@"%@ --- %@ ---",urlStr,code);
//        return NO;
    }
    return YES;
}

/**
 *  @author Calvin.wang, 15-05-17 20:05:36
 *
 *  access API
 *
 *  @param code <#code description#>
 */
-(void)accessTokenWithCode:(NSString *)code
{
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    params[@"client_id"]=AppKey;
//    params[@"client_secret"]=AppSecret;
//    params[@"grant_type"]= @"authorization_code";
//    params[@"code"] =code;
//    params[@"redirect_uri"] = RedirectURI;
//    [manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary*responseObject) {
//        NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//        NSString *filePath = [doc stringByAppendingPathComponent:@"account.plist"];
//        [responseObject writeToFile:filePath atomically:YES];
//        AccountModel *model =[[AccountModel alloc] init];
//        model.access_token = responseObject[@"access_token"];
//        model.expires_in = responseObject[@"expires_in"];
//        model.uid = responseObject[@"uid"];
//        [AccountTool save:model];
//        [ControllerTool chooseRootViewController];
//        NSLog(@"success --- %@",responseObject);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"failed --- %@",error);
//    }];
    
    AccessTokenParam *param = [[AccessTokenParam alloc] init];
    param.client_id = AppKey;
    param.client_secret = AppSecret;
    param.grant_type = @"authorization_code";
    param.code = code;
    param.redirect_uri = RedirectURI;
    [AccountTool accessTokenWithParam:param success:^(AccountModel *result) {
       
        [AccountTool save:result];
        [ControllerTool chooseRootViewController];
    } failure:^(NSError *error) {
        NSLog(@"failed --- %@",error);
    }];
}


@end
